import {request} from '../../request/index.js';
import regeneratorRuntime from '../../lib/runtime/runtime.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    leftMenuList: [],
    rightContent: [],
    currentActive: 0,
    scrollTop: 0
  },
  cates: [],

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const Cates = wx.getStorageSync("cates");
    if (!Cates) {
      this.getMenuList();
    } else {
      if (Date.now() - Cates.time > 1000 * 60 * 5)
        this.getMenuList();
      else {
        this.cates = Cates.data;
        let leftMenuList = this.cates.map(c => c.cat_name);
        let rightContent = this.cates[0].children;
        this.setData({
          leftMenuList,
          rightContent
        })
      }
    }
  },
  async getMenuList() {
    // requset({
    //   url: "/categories"
    // }).then(
    //   res => {
    //     this.cates = res;
    //     wx.setStorageSync("cates", {time:Date.now(),data:this.cates});
    //     let leftMenuList = this.cates.map(c => c.cat_name);
    //     let rightContent = this.cates[0].children;
    //     this.setData({
    //       leftMenuList,
    //       rightContent,
    //     })
    //   }
    // )

    const res=await request({
      url: "/categories"
    });
    this.cates = res;
    wx.setStorageSync("cates", {
      time: Date.now(),
      data: this.cates
    });
    let leftMenuList = this.cates.map(c => c.cat_name);
    let rightContent = this.cates[0].children;
    this.setData({
      leftMenuList,
      rightContent,
    })
  },
  handleIndex(e) {
    const {
      index
    } = e.currentTarget.dataset;
    let rightContent = this.cates[index].children;
    this.setData({
      currentActive: index,
      rightContent,
      scrollTop: 0
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})